package com.jettech.oa.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jettech.oa.dao.DocumentDAO;
import com.jettech.oa.dao.WorkFlowDAO;
import com.jettech.oa.domain.Document;
import com.jettech.oa.domain.DocumentApproveInfo;
import com.jettech.oa.domain.WorkFlow;
import com.jettech.oa.dto.WorkFlowCondition;
import com.jettech.oa.service.DocumentService;
import com.jettech.oa.service.JbpmService;
import com.jettech.oa.utils.PageUtil;

@Transactional
@Service("documentService")
public class DocumentServiceImpl implements DocumentService {
	@Autowired
	private DocumentDAO documentDAO;
	@Autowired
	private WorkFlowDAO workFlowDAO;
	@Autowired
	private JbpmService jbpmService;
	
	public void deleteDocument(Long id) {
		Document document = documentDAO.findById(id);
		if(document!=null){
			if(document.getProcessInstanceId()!=0){
				//删除流程实例
				jbpmService.deleteProcessInstance(document.getProcessInstanceId());
			}
			documentDAO.makeTransient(document);
		}
		
	}

	public List<Document> findDocumentsByWorkFlowCondition(
			WorkFlowCondition condition, PageUtil pageUtil) {
		return null;
	}

	@Transactional(propagation=Propagation.REQUIRED)
	public void saveDocument(Document document) {
		documentDAO.makePersistent(document);
		WorkFlow workFlow = workFlowDAO.findById(document.getWorkFlow().getId());
		//添加流程实例对象
		long processInstanceId = jbpmService.saveProcessInstance(workFlow.getName(), document.getId());
		//绑定流程实例标识到公文对象
		document.setProcessInstanceId(processInstanceId);
		documentDAO.makePersistent(document);
	}

	public void updateDocument(Document document) {
		documentDAO.makePersistent(document);
	}

	public void sumbmitDocument(Long documentId,Long userId,String loginName,String comment,String transitionName) {
		DocumentApproveInfo approveInfo =  new DocumentApproveInfo();
		Document doc = documentDAO.findById(documentId);
		approveInfo.setDocument(doc);
		com.jettech.oa.domain.User user = new com.jettech.oa.domain.User();
		user.setId(userId);
		approveInfo.setUser(user);
		approveInfo.setComment(comment);
		approveInfo.setApproveTiem(new Date());
		String nextNodeName = jbpmService.searchNextStep(loginName, doc.getProcessInstanceId(), transitionName);
		doc.setCurrentStateName(nextNodeName);
		documentDAO.makePersistent(doc);
	}

	@Transactional(readOnly=true)
	public List<Document> findUserWaitingAuditDocumentList(String loginName) {
		List<Long> documentIds = jbpmService.searchMyTashList(loginName);
		List<Document> documentList = documentDAO.findDocumentListByIds(documentIds);
		return documentList;
	}

	public List<String> findDocumentNextTransitionName(Long documentId,
			String loginName) {
		Document doc = documentDAO.findById(documentId);
		List<String> transitionNameList = jbpmService.searchNextTransitions(loginName, doc.getProcessInstanceId());
		return transitionNameList;
	}

}
